package cn.kxm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.dao.ParttypeDetailMapper;
import cn.kxm.pojo.OutstorageBill;
import cn.kxm.pojo.PartType;
import cn.kxm.pojo.ParttypeDetail;
import cn.kxm.service.ParttypeDetailService;
@Controller
@RequestMapping("/partdetail")
public class ParttypeDetailController extends BaseController{

	@Autowired
	private ParttypeDetailService ps;
	@RequestMapping("show")
	@ResponseBody
	public Object show(@RequestParam(value="findname",required=false)String pid,
			@RequestParam(value="findptid",required=false)String _ptid,
			Integer page,Integer rows){
		Integer ptid=0;
		if(_ptid==null||_ptid==""){
			ptid=0;
		}else{
			ptid=Integer.parseInt(_ptid);
		}
		Integer id=0;
		if(pid==null||pid==""){
			id=0;
		}else{
			id=Integer.parseInt(pid);
		}
		List<Map<String, Object>> list=ps.getall(id, ptid,(page-1)*rows,rows);
		int total=ps.getCount(id, ptid);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", total);
		return JSONArray.toJSONString(map);
	}
	
	@RequestMapping("getid")
	@ResponseBody
	public Object getid(){
		int x=ps.getid();//id最大值
		x=x+1;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("result", x);
		return JSON.toJSONString(map);
	}
	
	@RequestMapping("findone")
	public String findone(String code,Model model){
		Integer id=Integer.parseInt(code);
		Map<String, Object> map=ps.getoneById(id);
		model.addAttribute("parttype",map);
		return "web/goods/showparttypedetail";
	}
	@RequestMapping("modifyone")
	public String modifyone(String code,Model model){
		Integer id=Integer.parseInt(code);
		Map<String, Object> map=ps.getoneById(id);
		model.addAttribute("parttype",map);
		return "web/goods/modifyparttypedetail";
	}
	@RequestMapping("add")
	@ResponseBody
	public Object add(ParttypeDetail pd,String id,String typename){
		PartType p=new PartType();
		p.setId(Integer.parseInt(id));
		p.setTypeName(typename);
		Map<String, Object> map=new HashMap<String, Object>();
		pd.setPtid(Integer.parseInt(id));
		pd.setModifyuser("jemo_c");
		pd.setModifyDate(new Date());
		if(ps.adddouble(p, pd)){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	} 
	@RequestMapping("modify")
	@ResponseBody
	public Object modify(ParttypeDetail pd,String id,String typename){
		System.out.println(pd.getMemo());
		PartType p=new PartType();
		p.setId(Integer.parseInt(id));
		p.setTypeName(typename);
		Map<String, Object> map=new HashMap<String, Object>();
		pd.setPtid(Integer.parseInt(id));
		pd.setModifyuser("jemo_c");
		pd.setModifyDate(new Date());
		if(ps.updatedouble(p, pd)){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	} 
	//查重
	@RequestMapping("check")
	@ResponseBody
	public Object check(String name){
		Map<String, Object> map=new HashMap<String, Object>();
		List<PartType> l=ps.getlist();
		for (PartType p : l) {
			if(p.getTypeName().equals(name)){
				map.put("result", "error");
				return JSON.toJSONString(map);
			}
		}
		map.put("result", "success");
		return JSON.toJSONString(map);
	}
	//删除查重
	@RequestMapping("delcheck")
	@ResponseBody
	public Object delcheck(String id){
		int x=Integer.parseInt(id);
		Map<String, Object> map=new HashMap<String, Object>();
		List<Integer> list=ps.getarray();
		for (Integer i : list) {
			if(i==x){
				map.put("result", "error");
				return JSON.toJSONString(map);
			}
		}
		map.put("result", "success");
		return JSON.toJSONString(map);
	}

	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String code){
		Map<String, Object> map=new HashMap<String, Object>();
		if(ps.deldouble(code)){
			map.put("result", "success");
		}else{
			map.put("result", "error");
		}
		return JSON.toJSONString(map);
	}

}
